package com.ljt.business.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljt.business.entity.OutStock;
import com.ljt.business.service.OutStockService;
import com.ljt.response.Result;
import com.ljt.utils.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljt
 * @since 2021-07-03
 */
@RestController
@RequestMapping("/outStock")
public class OutStockController {
    @Autowired
    private OutStockService outStockService;

    @ApiOperation(value = "出库订单详细列表",notes = "分页查询出库单详细信息")
    @GetMapping("/queryAllOutStockByPage")
    public Result queryAllOutStockByPage(@RequestParam(required = false,defaultValue = "1") Integer current,
                                         @RequestParam(required = false,defaultValue = "6") Integer size){
        Page<OutStock> outStockPage=new Page<>(current,size);
        outStockPage=outStockService.page(outStockPage);
        return Result.ok().data("bizInStockPage",outStockPage);
    }
    @ApiOperation(value = "出库订单详细列表",notes = "修改出库单信息")
    @GetMapping("/updateOutStockById")
    public Result updateOutStockById(@RequestBody OutStock outStock){
        boolean i=outStockService.updateById(outStock);
        if(i){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @ApiOperation(value="出库订单详细列表",notes = "添加出库单信息")
    @GetMapping("/insertOutStockStock")
    public Result insertOutStock(@RequestBody OutStock outStock){
        boolean i=outStockService.save(outStock);
        if(i){
            return Result.ok().data("outStock",outStock);
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value = "导出",produces="application/octet-stream")
    @GetMapping("exportOutStockInfoToExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<OutStock> outStockList = outStockService.list();
        ExcelUtil<OutStock> util = new ExcelUtil<>(OutStock.class);
        util.exportExcel(response,outStockList , "出库订单数据");
    }

    @ApiOperation("导入")
    @PostMapping("importOutStockInfoToExcel")
    public Result importExcel(@RequestBody MultipartFile file) throws Exception {
        ExcelUtil<OutStock> util = new ExcelUtil<>(OutStock.class);
        List<OutStock> outStockList = util.importExcel(file.getInputStream());
        boolean insertFlag = outStockService.saveBatch(outStockList);
        if (insertFlag){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
}

